#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time : 2020/11/30 22:10
# @Author : 詹荣瑞
# @File : command.py
# @desc : 本代码未经授权禁止商用
from .core import ContentCore


class CommandLine(ContentCore):

    def __init__(self, command, op_para, re_para):
        self.command = command
        self.para = (op_para, re_para)

    def latex(self, indent=""):
        """

        :param indent: 缩进
        :return: LaTeX代码
        """
        op_para, re_para = self.para
        re_para = "".join(map(lambda p: f"{{{p}}}", re_para))
        op_para = ', '.join(op_para)
        if op_para != "":
            op_para = f"[{op_para}]"
        out = f"{indent}\\{self.command}{op_para}{re_para}\n"
        return out


class Command(object):

    def __init__(self, name):
        self.name = name

    def __call__(self, *args, **kwargs):
        kwargs = [f"{k} = {v}" for k, v in kwargs.items()]
        return CommandLine(self.name, kwargs, args)


title = Command("title")
document_class = Command("documentclass")
use_package = Command("usepackage")

begin = Command("begin")
end = Command("end")

section = Command("section")
